package com.aoip.telemetry.telemetry.receive.handler;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

import java.nio.charset.StandardCharsets;

/**
 * 心跳检查
 *
 * @author chenbai
 * @version 1.0
 * @date 2021/6/15 9:55
 */
public class HeartbeatHandler extends ChannelDuplexHandler {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state() == IdleState.READER_IDLE) {
            System.out.println("超时。。。。");
            ctx.channel().writeAndFlush("超时".getBytes(StandardCharsets.UTF_8));
            ctx.channel().close();
        }
    }

}
